package dimension.starry.loot;

import dimension.starry.utils.RandomUtil;
import net.minecraft.potion.Potion;
import net.minecraft.potion.Potions;

import java.util.HashMap;
import java.util.Map;

public class PotionLoots {

    private static final Map<Potion, Integer> LOOT_WEIGHT_MAP = new HashMap<>() {{
        put(Potions.EMPTY, 5000);
        put(Potions.WATER, 300);
        put(Potions.MUNDANE, 100);
        put(Potions.THICK, 100);
        put(Potions.AWKWARD, 100);
        put(Potions.NIGHT_VISION, 100);
        put(Potions.LONG_NIGHT_VISION, 10);
        put(Potions.INVISIBILITY, 100);
        put(Potions.LONG_INVISIBILITY, 10);
        put(Potions.LEAPING, 100);
        put(Potions.LONG_LEAPING, 10);
        put(Potions.STRONG_LEAPING, 10);
        put(Potions.FIRE_RESISTANCE, 100);
        put(Potions.LONG_FIRE_RESISTANCE, 10);
        put(Potions.SWIFTNESS, 100);
        put(Potions.LONG_SWIFTNESS, 10);
        put(Potions.STRONG_SWIFTNESS, 10);
        put(Potions.SLOWNESS, 100);
        put(Potions.LONG_SLOWNESS, 10);
        put(Potions.STRONG_SLOWNESS, 10);
        put(Potions.TURTLE_MASTER, 100);
        put(Potions.LONG_TURTLE_MASTER, 10);
        put(Potions.STRONG_TURTLE_MASTER, 10);
        put(Potions.WATER_BREATHING, 100);
        put(Potions.LONG_WATER_BREATHING, 10);
        put(Potions.HEALING, 100);
        put(Potions.STRONG_HEALING, 10);
        put(Potions.HARMING, 100);
        put(Potions.STRONG_HARMING, 10);
        put(Potions.POISON, 100);
        put(Potions.LONG_POISON, 10);
        put(Potions.STRONG_POISON, 10);
        put(Potions.REGENERATION, 100);
        put(Potions.LONG_REGENERATION, 10);
        put(Potions.STRONG_REGENERATION, 10);
        put(Potions.STRENGTH, 100);
        put(Potions.LONG_STRENGTH, 10);
        put(Potions.STRONG_STRENGTH, 10);
        put(Potions.WEAKNESS, 100);
        put(Potions.LONG_WEAKNESS, 10);
        put(Potions.LUCK, 10);
        put(Potions.SLOW_FALLING, 100);
        put(Potions.LONG_SLOW_FALLING, 10);
    }};

    public static Potion getRandomItem() {
        int totalWeight = LOOT_WEIGHT_MAP.values().stream().mapToInt(i -> i).sum();
        int random = RandomUtil.randomInt(1, totalWeight + 1), current = 0;
        for (Map.Entry<Potion, Integer> entry : LOOT_WEIGHT_MAP.entrySet()) {
            if (random > current && random <= current + entry.getValue()) {
                return entry.getKey();
            }
            current += entry.getValue();
        }
        throw new RuntimeException(String.format("calculate error, weight not in range: total=%d random=%d current=%d",
            totalWeight,
            random,
            current
        ));
    }

}
